﻿// // -------------------------------------------------------------------------------------
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

var dishdash = dishdash || { };
dishdash.widgetconfiguration = dishdash.widgetconfiguration || { };


dishdash.widgetconfiguration.saveAWidget = function(objWidget) {
    try {
        //If localStorage supports with the browser
        if (localStorage) {

            //Get the existing widget list if there is one
            var objWidgets = localStorage.getItem("Widgets");

            if (objWidgets != null && objWidgets != 'null') {
                //Parse the list
                var arWidgets = JSON.parse(objWidgets);
            } else {
                //create array to store list
                var arWidgets = { };
            }
            //Set name of widget as the key
            arWidgets[objWidget.widgetName] = objWidget;
            localStorage.setItem("Widgets", JSON.stringify(arWidgets));

        }
    } catch(err) {
        alert(err.Description);
    }
};


dishdash.widgetconfiguration.getAWidget = function(widgetname) {
    try {
        //If localStorage supports with the browser
        if (localStorage) {
            var sWidgets = localStorage.getItem("Widgets");

            var parsedResult = $.parseJSON(sWidgets);

            return parsedResult[widgetname];
        }
    } catch(err) {
        alert(err.Description);
    }

};

dishdash.widgetconfiguration.getAllWidgets = function() {
    try {
        //If localStorage supports with the browser
        if (localStorage) {
            var sWidgets = localStorage.getItem("Widgets");

            var parsedResult = $.parseJSON(sWidgets);

            return parsedResult;
        }
    } catch(err) {
        alert(err.Description);
    }

};


dishdash.widgetconfiguration.Widget = function() {
    this.widgetName = "";
    this.events = [];
    this.samplePeriod = 360;
    this.precision = "";
    this.refreshPeriod = 0;
    this.WidgetType = 0; //1 == Line Graph , 2 = Aggregated Event Count, 3 = Event Distribution
    this.GraphType = 0; // 1= Stacked Bar, 2 = Radar divGraphType
    this.ContextKey = "";
    this.ContextValue = "";
};

dishdash.widgetconfiguration.clearStorage = function() {
    if (localStorage) {
        localStorage.setItem("Widgets", null);
        alert("Cleared");
    }
};


dishdash.widgetconfiguration.WidgetTypeEnum = {
    LineGraph: 1,
    AggrEventCount: 2,
    EventDist: 3
};

dishdash.widgetconfiguration.GraphType = {
    StackedBar: 1,
    Radar: 2
};